package com.qijian.mold.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qijian.mold.domain.param.mold.GetEqpParam;
import com.qijian.mold.domain.param.mold.GetMoldChangeListByCurrentIdParam;
import com.qijian.mold.domain.param.mold.GetUserDetailByRoleTypeParam;
import com.qijian.mold.domain.param.mold.GetWorkOrderDetailsListParam;
import com.qijian.mold.domain.param.moldChange.*;
import com.qijian.mold.domain.param.task.CompleteMoldChangeParam;
import com.qijian.mold.domain.vo.ApiResultVo;
import com.qijian.mold.domain.vo.PageResult;
import com.qijian.mold.domain.vo.moldChange.*;
import com.qijian.mold.domain.vo.moldChangeLog.ChangeLogListVo;
import com.qijian.mold.domain.vo.user.UserDetailsVo;
import com.qijian.mold.enums.MoldRoleEnum;
import com.qijian.mold.service.MoldChangeLogService;
import com.qijian.mold.service.MoldChangeService;
import com.qijian.mold.service.MoldChangeTaskService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 换模信息
 * @author anni.zhu
 * @author anni.zhu@king-v.com
 * Created in 2025/3/29 19:38
 */
@Api(tags = "换模信息")
@RestController
@RequestMapping("/mold/change")
public class MoldChangeController {

    @Autowired
    private MoldChangeService service;
    @Autowired
    private MoldChangeTaskService taskService;

    /**
     * 新增换模信息
     * @param param
     * @return
     */
    @PostMapping("/addMoldChange")
    public ApiResultVo save(@RequestBody @Valid CreateMoldChangeParam param) {
        service.save(param);
        return ApiResultVo.success();
    }

    /**
     * 查看换模信息详情
     * @param id
     * @return
     */
    @GetMapping("/getMoldChange")
    public ApiResultVo<MoldChangeVo> getTrainCourse(@RequestParam("id") Long id) {
        MoldChangeVo vo = service.getById(id);
        return ApiResultVo.success(vo);
    }

    /**
     * 获取换模信息分页列表
     *
     * @return
     */
    @PostMapping("/getMoldChangeList")
    public PageResult<MoldChangeListVo> getMoldChangeList(@RequestBody SelectMoldChangeParam param) {
        Page<MoldChangeListVo> page = service.selectByPage(param);
        return PageResult.success(page);
    }
    /**
     * 根据设备id获取设备信息
     * @return
     */
    @GetMapping("/getEqpById")
    public ApiResultVo<EqpVo> getEqpById(@RequestParam("eqpId") Long eqpId) {
        EqpVo vo = service.getEqpById(eqpId);
        return ApiResultVo.success(vo);
    }

    /**
     * 根据设备code获取设备信息
     * @return
     */
    @GetMapping("/getEqpByCode")
    public ApiResultVo<EqpVo> getEqpByCode(@RequestParam("code") String code) {
        EqpVo vo = service.getEqpByCode(code);
        return ApiResultVo.success(vo);
    }

    /**
     * 获取设备列表isCheck是否显示进行中的设备
     * @return
     */
    @PostMapping("/getEqpList")
        public PageResult<EqpVo> getEqpList(@RequestBody GetEqpParam param) {
        if (param.getIsCheck() == null) {
            param.setIsCheck(false);
        }
        Page page = service.getEqpList(param);
        return PageResult.success(page);
    }

    /**
     * 获取工单列表
     * @return
     */
    @PostMapping("/getWorkOrderDetailsList")
    public PageResult<WorkOrderDetailsVo> getWorkOrderDetailsList(@RequestBody GetWorkOrderDetailsListParam param) {
        Page<WorkOrderDetailsVo> page = service.getWorkOrderDetailsList(param);
        return PageResult.success(page);
    }

    /**
     * 技术部确认
     * @param param
     * @return
     */
    @PostMapping("/departmentConfirm")
    public ApiResultVo departmentConfirm(@RequestBody @Valid ConfirmMoldChangParam param) {
        service.departmentConfirm(param);
        return ApiResultVo.success();
    }

    /**
     * 根据当前用户获取换模申请单数据
     * @return
     */
    @PostMapping("/getMoldChangeListByCurrentId")
    public PageResult<MoldChangeDownVo> getMoldChangeList(@RequestBody GetMoldChangeListByCurrentIdParam param) {
        Page<MoldChangeDownVo> page = service.getMoldChangeList(param);
        return PageResult.success(page);
    }

    /**
     * 换模申请的开始换模调试完成
     * @param param
     * @return
     */
    @PostMapping("/moldChangeOverDebug")
    public ApiResultVo moldChangeOverDebug(@RequestBody @Valid CompleteMoldChangeParam param) {
        service.moldChangeOverDebug(param);
        return ApiResultVo.success();
    }

    /**
     * 车间确认完成
     * @param param
     * @return
     */
    @PostMapping("/workshopConfirm")
    public ApiResultVo workshopConfirm(@RequestBody @Valid WorkshopConfirmMoldChangParam param) {
        service.workshopConfirm(param);
        return ApiResultVo.success();
    }

    /**
     * 删除换模申请
     * @param moldChangeId
     * @return
     */
    @GetMapping("/deleteMoldChange")
    public ApiResultVo deleteMoldChange(@RequestParam("moldChangeId") Long moldChangeId) {
        taskService.deleteMoldChange(moldChangeId);
        return ApiResultVo.success();
    }

    /**
     * 根据设备id获取操作员
     * @return
     */
    @PostMapping("/getUserDetailByEqpId")
    public PageResult<UserDetailsVo> getUserDetailByEqpId(@RequestBody GetUserDetailByRoleTypeParam param) {
        param.setRoleType(MoldRoleEnum.WORKER.getId());
        Page<UserDetailsVo> page = service.getUserDetailByRoleType(param);
        return PageResult.success(page);
    }


    /**
     * 根据角色类型获取用户 1-技术员 2-车间操作员
     * @return
     */
    @PostMapping("/getUserDetailByRoleType")
    public PageResult<UserDetailsVo> getUserDetailByRoleType(@RequestBody GetUserDetailByRoleTypeParam param) {
        Page<UserDetailsVo> page = service.getUserDetailByRoleType(param);
        return PageResult.success(page);
    }

    /**
     * 修改换模信息可见人ids
     * @param param
     * @return
     */
    @PostMapping("/updateMoldVisibleUserIds")
    public ApiResultVo updateMoldVisibleUserIds(@RequestBody @Valid UpdateMoldVisibleUserIdsParam param) {
        service.updateMoldVisibleUserIds(param);
        return ApiResultVo.success();
    }
}
